//
//  MineViewController.m
//  YunGou
//
//  Created by macbook on 16/5/16.
//  Copyright © 2016年 macbook. All rights reserved.
//

#import "MineViewController.h"
#import "SettingViewController.h"
#import "ShowOrderViewController.h"
#import "BuyRecordViewController.h"
#import "WinRecordViewController.h"
#import "RechargeRecordViewController.h"
#import "ReceiveAddressViewController.h"
#import "RechargeViewController.h"
#import "BlessingsController.h"
#import "LoginViewController.h"
#import "UIBarButtonItem+Extension.h"
#import "RootNavViewController.h"
#import <UIButton+WebCache.h>

@interface MineViewController ()
@property (weak, nonatomic) IBOutlet UIButton *headImg;
@property (weak, nonatomic) IBOutlet UILabel *usernameLabel;
@property (weak, nonatomic) IBOutlet UIButton *chargeBtn;
@property (weak, nonatomic) IBOutlet UILabel *moneyLabel;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createNav];
    _headImg.layer.cornerRadius = _headImg.frame.size.width/2;
    _headImg.clipsToBounds = YES;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    //如果登录
    if (IsLogin) {
        _loginBtn.hidden = YES;
        _chargeBtn.hidden = NO;
        _moneyLabel.hidden = NO;
        _usernameLabel.hidden = NO;
        if (![[UserModel sharedInstance].img isEqualToString:@""] && [UserModel sharedInstance].img) {
            [_headImg sd_setBackgroundImageWithURL:[NSURL URLWithString:[ImageUrl stringByAppendingString:[UserModel sharedInstance].img]] forState:UIControlStateNormal];
        }else{
            [_headImg setBackgroundImage:[UIImage imageNamed:@"sdfx_toux"] forState:UIControlStateNormal];
        }
        
        if (![[UserModel sharedInstance].username isEqualToString:@""]) {
            _usernameLabel.text = [UserModel sharedInstance].username;
        }else{
            _usernameLabel.text = @"未设置";
        }
        
        [NetWorkTool userAccountWithSuccess:^(id object) {
            if ([object[@"code"] integerValue] == 1) {
                NSMutableAttributedString *money = [[NSMutableAttributedString alloc] initWithString: [NSString stringWithFormat:@"余额：%@元",object[@"data"]]];
                [money addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"f36e21"] range:NSMakeRange(3,money.length - 3)];
                
                _moneyLabel.attributedText = money;

        }
        } failure:^(NSError *error) {
            
        }];

        
        
            }else{
        [_headImg setBackgroundImage:[UIImage imageNamed:@"sdfx_toux"] forState:UIControlStateNormal];
        _chargeBtn.hidden = YES;
        _moneyLabel.hidden = YES;
        _usernameLabel.hidden = YES;
        _loginBtn.hidden = NO;
    }
}

- (void)createNav{
    UIBarButtonItem *rightBarItem = [UIBarButtonItem barButtonTitle:nil image:@"wd_shezhi" frame:CGRectMake(0, 0, 20, 20) target:self sel:@selector(jumpToSetting)];
    self.navigationItem.rightBarButtonItem = rightBarItem;
}

- (void)jumpToSetting{
    SettingViewController *settingVC = [[SettingViewController alloc] init];
    [self.navigationController pushViewController:settingVC animated:YES];
}

//跳转到充值界面
- (IBAction)chargeClicked:(id)sender {
    if (!IsLogin) {
        LoginViewController *loginVC = SB(@"Login", @"LoginVC");
        [self.navigationController pushViewController:loginVC animated:YES];
        return;
    }
    RechargeViewController *rechargeVC = SB(@"Mine", @"RechargeVC");
    [self.navigationController pushViewController:rechargeVC animated:YES];
}
- (IBAction)loginClicked:(id)sender {
    LoginViewController *loginVC = SB(@"Login", @"LoginVC");
    loginVC.isPresent = YES;
    RootNavViewController *nav = [[RootNavViewController alloc] initWithRootViewController:loginVC];
    [self presentViewController:nav animated:YES completion:nil];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 3;
    }else if(section == 1){
        return 2;
    }return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0.01f;
    }return 5;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (!IsLogin) {
        LoginViewController *loginVC = SB(@"Login", @"LoginVC");
        [self.navigationController pushViewController:loginVC animated:YES];
        return;
    }
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            //云购记录
            BuyRecordViewController *recordVC = SB(@"Mine", @"RecordVC");
            [self.navigationController pushViewController:recordVC animated:YES];
        }else if (indexPath.row == 1){
            //中奖记录
            WinRecordViewController *winRecordVC = SB(@"Mine", @"WinRecordVC");
            [self.navigationController pushViewController:winRecordVC animated:YES];
        }else{
            //充值记录
            RechargeRecordViewController *rechargeRecordVC = SB(@"Mine", @"RechargeRecordVC");
            [self.navigationController pushViewController:rechargeRecordVC animated:YES];
        }
    }else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
//            我的福分
            BlessingsController *blessingVC = SB(@"Mine", @"BlessingVC");
            [self.navigationController pushViewController:blessingVC animated:YES];
        }else{
            //我的晒单
            ShowOrderViewController *showOrderVC = SB(@"Common", @"ShowOrderVC");
            showOrderVC.title = @"我的晒单";
            showOrderVC.isMine = YES;
            showOrderVC.isShare = NO;
            [self.navigationController pushViewController:showOrderVC animated:YES];
        }
    }else{
        //收获地址
        ReceiveAddressViewController *receiveAddressVC = SB(@"Mine", @"ReceiveAddressVC");
        [self.navigationController pushViewController:receiveAddressVC animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
